package com.huangj.util;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.huangj.entity.HomeEntity;
import com.huangj.entity.HomeInfoEntity;
import com.huangj.entity.HomePics;
import com.huangj.entity.TravelEntity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by HuangJ on 2016/10/2.23:37
 */
public class JsonUtil {
    //解析顶部的图片;
    public static List<HomePics> getHomePicsJson(String json){
        List<HomePics> picList = new ArrayList<>();
        try {
            JSONArray datas = new JSONObject(json).getJSONArray("data");
            for (int i = 0; i < datas.length();i++ ){
                HomePics homePics = new HomePics();
                JSONObject jsonobject = datas.getJSONObject(i).getJSONObject("photo");
                String pic_url = jsonobject.getString("photo_url");
                homePics.setPhoto_url(pic_url);
                picList.add(homePics);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return picList;
    }

    //解析主页面的数据；
    public static HomeEntity getHomeEntityJson(String json){
//        Log.d("print", "getHomeEntityJson: "+new Gson().fromJson(json,HomeEntity.class));
        return new Gson().fromJson(json,HomeEntity.class);
    }

    //解析游记页面；
    public static List<TravelEntity> getTravelEntityJson(String json){
        try {
            JSONArray ja = new JSONObject(json).getJSONArray("data");
            TypeToken<List<TravelEntity>> tt = new TypeToken<List<TravelEntity>>(){};
            Log.d("print", "游记的JSON "+new Gson().fromJson(ja.toString(),tt.getType()));
            return new Gson().fromJson(ja.toString(),tt.getType());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    //解析首页详情；
    public static HomeInfoEntity getHomeInfoEntityJson(String json){
        return new Gson().fromJson(json,HomeInfoEntity.class);
    }
}
